package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.exception.ServiceException;
import com.mall.pojo.GoodsPo;
import com.mall.mapper.GoodsMapper;
import com.mall.service.GoodsService;
import com.mall.vo.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zhangshiqi
 * @since 2020-09-13
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    private List<GoodsPo> goodsPoList;

    /**分页查询商品数据*/
    @Override
    public PageObject<GoodsPo> doFindPageObject(String username, Integer pageCurrent) {
        //1.参数校验
        if(pageCurrent == null || pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确");
        //2.PageHelper
        Page<Object> page = PageHelper.startPage(pageCurrent, 5);
        QueryWrapper<GoodsPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("state", "回收站");
        if (username==null){
            goodsPoList = goodsMapper.selectList(queryWrapper);
        }else {
            queryWrapper.like("name", username);
            goodsPoList = goodsMapper.selectList(queryWrapper);
        }
        //3.封装结果
        return new PageObject<GoodsPo>( page.getTotal(), goodsPoList, (long) page.getPages(), 5, pageCurrent);
    }

    /**将商品数据放入回收站*/
    @Override
    public void doRecycleObjects(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        for (String s : id) {
            goodsMapper.updateById(goodsMapper.selectById(s).setState("回收站"));
        }
    }

    /**新增商品数据*/
    @Override
    public void doAddObjects(GoodsPo goodsPo) {
        //1.参数校验
        if (goodsPo == null)
            throw new IllegalArgumentException("数据不正确");
        if (StringUtils.isEmpty(goodsPo.getName()))
            throw new IllegalArgumentException("商品名称不能为空");
        if (goodsPo.getPrice()<=0)
            throw new IllegalArgumentException("商品价格不能小于等于0");
        //2.添加默认值
        goodsPo.setState("待审核").setDisplay("下架");
        //3.语句执行
        int rows = goodsMapper.insert(goodsPo);
        if (rows==0)
            throw new ServiceException("商品添加失败");
    }

    /**修改商品数据*/
    @Override
    public void doUpdateObjects(GoodsPo goodsPo) {
        //1.参数校验
        if (goodsPo == null)
            throw new IllegalArgumentException("数据不正确");
        if (StringUtils.isEmpty(goodsPo.getName()))
            throw new IllegalArgumentException("商品名称不能为空");
        if (goodsPo.getPrice()<=0)
            throw new IllegalArgumentException("商品价格不能小于等于0");
        //2.语句执行
        int rows = goodsMapper.updateById(goodsPo);
        if (rows==0)
            throw new ServiceException("没有该商品数据");
    }

    /**分页查询回收站商品数据*/
    @Override
    public PageObject<GoodsPo> doFindRecycleObjects(String username, Integer pageCurrent) {
        //1.参数校验
        if(pageCurrent == null || pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确");
        //2.PageHelper
        Page<Object> page = PageHelper.startPage(pageCurrent, 5);
        QueryWrapper<GoodsPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "回收站");
        if (username==null){
            goodsPoList = goodsMapper.selectList(queryWrapper);
        }else {
            queryWrapper.like("name", username);
            goodsPoList = goodsMapper.selectList(queryWrapper);
        }
        //3.封装结果
        return new PageObject<GoodsPo>( page.getTotal(), goodsPoList, (long) page.getPages(), 5, pageCurrent);
    }

    /**删除回收站商品数据*/
    @Override
    public void doDeleteRecycleObjects(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        List<String> idList = new ArrayList<>();
        Collections.addAll(idList, id);
        goodsMapper.deleteBatchIds(idList);
    }

    /**恢复回收站中商品数据*/
    @Override
    public void doRecoveryObjects(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        for (String s : id) {
            goodsMapper.updateById(goodsMapper.selectById(s).setState("未上架").setDisplay("下架"));
        }
    }

    /**根据ID查询商品数据*/
    @Override
    public GoodsPo doFindGoodsById(Integer id) {
        //1.参数校验
        if (id == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.执行语句
        return goodsMapper.selectById(id);
    }

    /**上架商品*/
    @Override
    public void doChangeDisplayTrue(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        for (String s : id) {
            goodsMapper.updateById(goodsMapper.selectById(s).setDisplay("上架"));
        }
    }

    /**下架商品*/
    @Override
    public void doChangeDisplayFalse(String ids) {
        //1.参数校验
        if (ids == null)
            throw new IllegalArgumentException("ID值不正确");
        //2.解析参数
        String[] id = ids.split(",");
        //3.语句执行
        for (String s : id) {
            goodsMapper.updateById(goodsMapper.selectById(s).setDisplay("下架"));
        }
    }

    /**根据商品状态显示商品*/
    @Override
    public PageObject<GoodsPo> doSelectByState(String className, Integer pageCurrent) {
        //1.参数校验
        if(pageCurrent == null || pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确");
        //2.PageHelper
        Page<Object> page = PageHelper.startPage(pageCurrent, 5);
        QueryWrapper<GoodsPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", className);
        goodsPoList = goodsMapper.selectList(queryWrapper);
        //3.封装结果
        return new PageObject<GoodsPo>( page.getTotal(), goodsPoList, (long) page.getPages(), 5, pageCurrent);
    }
}
